<?php
/**
 * Created by Command.
 * User: Vito
 */

declare (strict_types = 1);

namespace app\logic;

use app\model\Room;
use app\model\RoomLeaseRecord;
use app\model\Housing;
class RoomLogic extends BaseLogic
{

    /**
     * 设置模型
     * @return string
     */
    protected function getModelName(): string
    {
        return Room::class;
    }

    /**
     * @Description: 获取房间信息
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-14 13:42:30
     * @version: 1.0.0
     * @return [type]
     */    
    public static function getRoom($where){

        return Room::getRoom($where);
    }
    /**
     * @Description: 获取房间列表
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-22 09:43:58
     * @version: 1.0.0
     * @return [type]
     */
    public static function getRoomList($params){

        $where['building_number'] = $params['id'];

        $res = Room::where($where)->select();

        return $res;
    }

    
    /**
     * @Description: 统计房间数
     * @author: xiayu <cb1071403016@126.com>
     * @param [type] $where         查询条件
     * @Date: 2022-12-05 10:08:44
     * @version: 1.0.0
     * @return [type]
     */    
    public function countRoon($type ='',$where = []){
        if($type != ''){
            $where[] = ['use_people','<>',0];
            return Room::where($where)->count();
        }
        return Room::where($where)->count();
    }

    /**
     * @Description: 入住人数
     * @author: xiayu <cb1071403016@126.com>
     * @param [type] $where
     * @Date: 2022-12-05 10:34:50
     * @version: 1.0.0
     * @return [type]
     */
    public function countAudit($type = '',$where = []){
        // halt($type);
        if($type != ''){
            $where['status'] = $type;
            return RoomLeaseRecord::where($where)->count();
        }
        return RoomLeaseRecord::where($where)->count();
    }
    
    /**
     * @Description: 获取全部小区房间总数
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-12-05 10:49:26
     * @version: 1.0.0
     * @return [type]
     */    
    public function getHousingRoomCount(){
        $Housing = Housing::select()->toArray();

        foreach ($Housing as $key => $value) {
             $where = [];
             $Housing[$key]['count_list'] = array();
             $Housing[$key]['count_list_name'] = array();
             $where[] = ['housing_id','=',$value['id']];
             $Housing[$key]['count_list'][0]['value'] = $this->countRoon('use',$where);
             $Housing[$key]['count_list'][0]['name'] = '已使用房间';
             $Housing[$key]['room_count'] = $this->countRoon('',$where);
             $where[] = ['use_people','=',0];
             $Housing[$key]['count_list'][1]['value'] = $this->countRoon('', $where);
             $Housing[$key]['count_list'][1]['name'] = '空闲房间';
             $Housing[$key]['count_list_name'][] = '已使用房间';
             $Housing[$key]['count_list_name'][] = '空闲房间';

            //  array_merge([$Housing[$key]['no_room_count'], $Housing[$key]['use_room_count']]);
             
        }
        return $Housing;
    }
}
